package richbar.com.richbar.activity.me;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.activity.login.RegisterAc;
import richbar.com.richbar.bean.BaseBean;
import richbar.com.richbar.bean.CodeBean;
import richbar.com.richbar.help.CountDownTimerHelp;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.dialog.TipDialog;

/**
 * Created by QiYang on 2018/11/1.
 */

public class ChangePayPwdAc extends BaseActivity {
    @BindView(R.id.new_pay_pwd)
    EditText new_pay_pwd;//旧的
    @BindView(R.id.original_pay_pwd)
    EditText original_pay_pwd;//新的
    @BindView(R.id.tel)
    EditText tel;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.get_code)
    TextView get_code;
    private LoadingDialog dialog;

    @Override
    public int initLayout() {
        return R.layout.ac_change_pay_pwd;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("修改支付密码").setRightTv(getString(R.string.finish), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                TipDialog.show(ChangePayPwdAc.this, "修改支付密码成功！", new TipDialog.OnListener() {
//                    @Override
//                    public void onBesure() {
//
//                    }
//                });
                commit();

            }
        });
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        dialog = new LoadingDialog(ChangePayPwdAc.this, 0);
    }

    @OnClick({R.id.get_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.get_code:
                if (tel.getText().toString().trim().length() == 11) {
                    if (!Util.checkNet(getApplicationContext())) {
                        ToastUtil.showToast(getApplicationContext(), getString(R.string.error_net));
                    } else {
                        getCode();
                        NetWorkUtil.getRegsinCode(Util.md5(), tel.getText().toString(), "6", new HttpUtils.HttpCallback() {
                            @Override
                            public void onSuccess(String data) {
                                final CodeBean codeBean = MyGson.getInstance().fromJson(data, CodeBean.class);
                                ToastUtil.showToast(ChangePayPwdAc.this, codeBean.getMsg());
                            }

                            @Override
                            public void onError(String msg) {
                                super.onError(msg);
                            }
                        });
                    }


                } else {
                    ToastUtil.showToast(ChangePayPwdAc.this, "请输入正确的手机号");
                }

                break;
//            case R.id.forget_pay_pwd:
//                start(ForgetPayPwdAc.class);
//                break;
        }
    }

    private void getCode() {
        new CountDownTimerHelp(this, get_code, 60000, 1000).start();
    }

    private void commit() {
        if (new_pay_pwd.getText().toString().length() != 6) {
            ToastUtil.showToast(ChangePayPwdAc.this, "请输入六位数字密码");
        } else {
            commit2Net();
        }
    }

    private void commit2Net() {
        if (tel.getText().toString().trim().length() == 11) {
            if (code.getText().toString().length() > 0) {
                if (!Util.checkNet(getApplicationContext())) {
                    ToastUtil.showToast(getApplicationContext(), getString(R.string.error_net));
                } else {
                    if (new_pay_pwd.getText().toString().length() == 6) {
                        dialog.show();
                        NetWorkUtil.updatesPayPwd(tel.getText().toString(), code.getText().toString(), original_pay_pwd.getText().toString(), new_pay_pwd.getText().toString(), new HttpUtils.HttpCallback() {
                            @Override
                            public void onSuccess(String data) {
                                dialog.dismiss();
                                final BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                                if (baseBean.getCode().equals("200"))
                                    finish();
                                ToastUtil.showToast(ChangePayPwdAc.this, baseBean.getMsg());
                            }

                            @Override
                            public void onError(String msg) {
                                super.onError(msg);
                                dialog.dismiss();
                            }
                        });
                    } else

                    {
                        ToastUtil.showToast(ChangePayPwdAc.this, "请输入六位数字密码");
                    }

                }
            } else {
                ToastUtil.showToast(ChangePayPwdAc.this, "请输入验证码");
            }
        } else {
            ToastUtil.showToast(ChangePayPwdAc.this, "请输入正确的手机号");
        }

    }

}
